package io.github.talelin.latticy.controller.v1.K;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.K.JcDwxxbDTO;
import io.github.talelin.latticy.model.K.JcDwxxbDO;
import io.github.talelin.latticy.service.K.JcDwxxbService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

/**
* @author generator@TaleLin
* @since 2021-06-08
*/
@RestController
@RequestMapping("/v1/jc-dwxxb")
public class JcDwxxbController {

    @Autowired
    private JcDwxxbService JcDwxxbService;

    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated JcDwxxbDTO dto) {
        JcDwxxbDO JcDwxxbDO = new JcDwxxbDO();
        BeanUtils.copyProperties(dto,JcDwxxbDO);
        this.JcDwxxbService.save(JcDwxxbDO);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @PermissionMeta("更新")
    @GroupRequired
    public UpdatedVO update(@RequestBody @Validated JcDwxxbDTO dto,
                            @PathVariable @Positive(message = "{id.positive}") Long id) {
        JcDwxxbService.update(dto, id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        JcDwxxbService.delete(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public JcDwxxbDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        JcDwxxbDO b_data = JcDwxxbService.getById(id);
        if (b_data == null) {
            throw new NotFoundException("b_gateway not found", 10022);
        }
        return b_data;
    }

    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO<JcDwxxbDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @Min(value = 0) @Max(value = 1) Integer root
    ) {
        IPage<JcDwxxbDO> paging = JcDwxxbService.getCategoriesByPage(count, page, root);
        return PageUtil.build(paging);
    }

    @GetMapping("/sub-page")
    @LoginRequired
    public PageResponseVO<JcDwxxbDO> subPage(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "id") @Positive(message = "{id}") Integer id
    ) {
        IPage<JcDwxxbDO> paging = JcDwxxbService.getSubCategoriesByPage(count, page, id);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<JcDwxxbDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "keyword") String keyword
    ) {
        Page<JcDwxxbDO> pager = new Page<>(page, count);
        IPage<JcDwxxbDO> paging = JcDwxxbService.seachJcDwxxb(pager,keyword);
        return PageUtil.build(paging);
    }

}
